﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TestingConcurrencyBenchmark.Helpers
{
    /// <summary>
    /// A counter that can not be approached in a thread safe way
    /// </summary>
    public class UnSafeCounter
    {
        /// <summary>
        /// Creates a new unsafecounter with an initial value
        /// | new.Count == initialValue
        /// </summary>
        /// <param name="initialValue">the internal counter will be set to this value</param>
        public UnSafeCounter(int initialValue)
        {
            Count = initialValue;
        }

        private volatile int _count;

        /// <summary>
        /// Returns the internal counter's value
        /// </summary>
        public int Count
        {
            get { return _count; }
            private set { _count = value; }
        }

        /// <summary>
        /// Adds an amount to this counter
        /// </summary>
        /// <param name="amount">The amount to add (not thread safe!)</param>
        public void Add(int amount)
        {
            Count += amount;
        }

        /// <summary>
        /// Subtracts an amount from this counter
        /// </summary>
        /// <param name="amount">The amount to subtract (not thread safe!)</param>
        public void Subtract(int amount)
        {
            Count -= amount;
        }
    }
}
